package com.bosssoft.hr.train.spring.example;



import com.bosssoft.hr.train.spring.example.bean.User;
import com.bosssoft.hr.train.spring.example.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class App {
    /**
     * 方法已过时：：
    public static void getBeanByBeanFactory(){
        //使用bean factory(过时)
        BeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource("spring-config.xml"));
        UserService userService = (UserService) beanFactory.getBean("userService");
        User user = new User();
        user.setName("user-bean-factory");
        userService.save(user);
    }
     **/

    public void getBeanByApplicationContext(){
        ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
        for (int i = 0; i < 3; i++) {
            UserService userService = (UserService) context.getBean("userService");
            User user = new User();
            user.setName("user-application-context");
            userService.save(user);
        }

        //关闭
        ((ClassPathXmlApplicationContext) context).close();

    }




}
